import { ref } from 'vue'
import type { BasicColumn, FormSchema } from '@/components/Table'
import { useRender } from '@/components/Table'
import { dictTypeApi } from '@/api/sys/dictTypeApi'

const colorOptions = [
  {
    value: '',
    label: '无',
  },
  {
    value: 'processing',
    label: 'processing',
  },
  {
    value: 'success',
    label: 'success',
  },
  {
    value: 'default',
    label: 'default',
  },
  {
    value: 'warning',
    label: 'warning',
  },
  {
    value: 'error',
    label: 'error',
  },
  {
    value: 'pink',
    label: 'pink',
  },
  {
    value: 'red',
    label: 'red',
  },
  {
    value: 'orange',
    label: 'orange',
  },
  {
    value: 'green',
    label: 'green',
  },
  {
    value: 'cyan',
    label: 'cyan',
  },
  {
    value: 'blue',
    label: 'blue',
  },
  {
    value: 'purple',
    label: 'purple',
  },
]

function previewOptions() {
  return colorOptions.map((option) => {
    const { value, label } = option
    if (value === '')
      return option

    return {
      label: useRender.renderTag(label, value),
      value,
    }
  })
}

export const columns: BasicColumn[] = [
  {
    title: '字典标签',
    dataIndex: 'dictName',
    width: 120,
  },
  {
    title: '字典值',
    dataIndex: 'dictValue',
  },
  {
    title: '字典编码',
    dataIndex: 'dictCode',
  },
  {
    title: '系统内建',
    dataIndex: 'buildIn',
    customRender: ({ text }) => useRender.renderDictTag(text, 'YES_OR_NO'),
  },
  {
    title: '颜色',
    dataIndex: 'dictMeta.color',
    customRender: ({ record: { dictMeta } }) => useRender.renderTag(dictMeta?.color, dictMeta?.color),
  },
  {
    title: '排序',
    dataIndex: 'sort',
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: '字典标签',
    field: 'dictName',
    component: 'Input',
    colProps: { span: 8 },
  },
]

const params = ref({})

export function setDictTypeTreeParams(model: Recordable) {
  params.value = model
  return params
}

const dictTypeIdRef = ref('')
export function useDictTypeId() {
  return {
    getDictTypeId: () => dictTypeIdRef.value,
    setDictTypeId: (dictTypeId: string) => {
      dictTypeIdRef.value = dictTypeId
    },
  }
}

export const dictTypeFormSchema: FormSchema[] = [
  {
    field: 'dictTypeId',
    show: false,
    component: 'Input',
  },
  {
    label: '字典类型 Code',
    field: 'dictTypeCode',
    component: 'Input',
    required: ({ values }) => !values.dictTypeId,
    dynamicDisabled: ({ values }) => !!values.dictTypeId,
  },
  {
    label: '上级',
    field: 'parentDictTypeId',
    required: false,
    component: 'ApiTreeSelect',
    componentProps: {
      params,
      api: dictTypeApi.tree,
      parentFiled: 'parentDictTypeId',
      valueField: 'dictTypeId',
      labelField: 'dictTypeName',
      resultField: 'rows',
    },
  },
  {
    label: '名称',
    field: 'dictTypeName',
    required: true,
    component: 'Input',
  },
  {
    label: '显示顺序',
    field: 'sort',
    required: true,
    defaultValue: 0,
    component: 'InputNumber',
  },
  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    required: false,
    componentProps: {
      placeholder: '请输入备注',
    },
  },
]

export const formSchema: FormSchema[] = [
  {
    field: 'dictId',
    show: false,
    component: 'Input',
  },
  {
    label: '字典类型',
    field: 'dictTypeId',
    required: false,
    component: 'ApiTreeSelect',
    dynamicDisabled: true,
    componentProps: {
      api: dictTypeApi.tree,
      parentFiled: 'parentDictTypeId',
      valueField: 'dictTypeId',
      labelField: 'dictTypeName',
      resultField: 'rows',
    },
  },
  {
    label: '字典标签',
    field: 'dictName',
    required: true,
    component: 'Input',
  },
  {
    label: '字典值',
    field: 'dictValue',
    required: true,
    component: 'Input',
    dynamicDisabled: ({ values }) => !values.buildIn,
  },
  {
    label: '字典编码',
    field: 'dictCode',
    required: true,
    component: 'Input',
    dynamicDisabled: ({ values }) => !values.buildIn,
  },
  {
    label: '颜色类型',
    field: 'dictMeta.color',
    component: 'Select',
    componentProps: {
      options: previewOptions(),
    },
  },
  {
    label: 'CSS Class',
    field: 'dictMeta.cssClass',
    component: 'Input',
    rules: [{ required: false, message: '只能是合法的css class标识', pattern: /^[\w-]+$/, trigger: 'blur' }],
  },
  {
    label: '显示顺序',
    field: 'sort',
    required: true,
    defaultValue: 0,
    component: 'InputNumber',
  },
  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    required: false,
    componentProps: {
      placeholder: '请输入备注',
    },
  },
]
